package com.yunxi.service.dubboService.impl.activiti;


import com.yunxi.activiti.pojo.ActivitiManageVO;
import com.yunxi.activiti.service.ActivitiManageDubboService;
import com.yunxi.activiti.service.ActivitiUnionDubboService;
import com.yunxi.core.base.Constant;
import com.yunxi.core.web.LoginInfo;
import com.yunxi.model.activiti.ActivitiManage;
import com.yunxi.model.activiti.ActivitiRecord;
import com.yunxi.service.localService.activiti.ActivitiDeployService;
import com.yunxi.service.localService.activiti.ActivitiManageService;
import com.yunxi.service.localService.activiti.ActivitiRecordService;
import org.activiti.engine.ProcessEngine;
import org.activiti.engine.ProcessEngines;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.repository.ProcessDefinition;
import org.activiti.engine.runtime.ProcessInstance;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by Administrator on 2018/2/11.
 */
@Service
public class ActivitiUnionDubboServiceImpl implements ActivitiUnionDubboService {

    ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();

    @Autowired
    @Qualifier(value = "activitiManageServiceImpl")
    ActivitiManageService activitiManageService;
    @Autowired
    @Qualifier(value = "activitiDeployServiceImpl")
    ActivitiDeployService activitiDeployService;
    @Autowired
    TaskService taskService;
    @Autowired
    RuntimeService runtimeService;
    @Autowired
    @Qualifier(value = "activitiRecordServiceImpl")
    ActivitiRecordService activitiRecordService;

    /**
     * 启动流程实例
     */
    public Map executionActiviti(String modelId, Long activitiTypeId,Long money, Long fileType,LoginInfo loginInfo) {
        Map map = new HashMap();
        String deploymentId = "";
        ActivitiManageVO deployActivitiManage = null;
        try {
            //通过公司id,部门id,职位id,工作流类型找到最新的部署id
//            map.put("companyId", wxOaDoc.getCompanyId());
//            map.put("deptId", wxOaDoc.getDeptId());
//            map.put("positionId", wxOaDoc.getPositionId());
            map.put("activitiTypeId",activitiTypeId);
            map.put("modelId",modelId);
//            deployActivitiManage = activitiManageService.findNewsDeploymentId(map);
            if (deployActivitiManage != null) {
                deploymentId = String.valueOf(deployActivitiManage.getActivitiDeployId());
            }
            ProcessDefinition processDefinition = processEngine.getRepositoryService()//与流程定义和部署对象相关的Service
                    .createProcessDefinitionQuery()//创建一个流程定义查询
                    .deploymentId(deploymentId)//使用部署对象ID查询
                    .singleResult();
            //设置参数进行办理,将任务提交到下个节点
            Map<String, Object> variables = new HashMap<String, Object>();
            //设置多个键值对
            ProcessInstance processInstance = null;
            if(fileType ==null && money == null){
                //这个流程没有条件,没有网关
                 processInstance = processEngine.getRuntimeService().startProcessInstanceByKey(processDefinition.getKey());

            }
            if(fileType !=null && money != null){
                //关于钱的条件
                variables.put("fileType",fileType);
                variables.put("money",money);
                processInstance = processEngine.getRuntimeService().startProcessInstanceByKey(processDefinition.getKey(), variables);

            }
            if(fileType ==null && money != null){
                //关于钱的条件,其他条件
                variables.put("money",money);
                processInstance = processEngine.getRuntimeService().startProcessInstanceByKey(processDefinition.getKey(), variables);

            }
            if(fileType !=null && money == null){
                //其他条件
                variables.put("fileType",fileType);
                processInstance = processEngine.getRuntimeService().startProcessInstanceByKey(processDefinition.getKey(), variables);

            }
            System.out.println("实例Id" + processInstance.getId() + "   " + processInstance.getName() + "  " + processInstance.getProcessDefinitionId()
                    + "  " + processInstance.getActivityId());
            //插入一条记录,记录申请
            ActivitiRecord activitiRecord = new ActivitiRecord();
            activitiRecord.setApplyUserId(loginInfo.getUserId());
            activitiRecord.setAuditStatus(Constant.AUDITING);
            activitiRecord.setApplyDate(new Date());
            activitiRecord.setActivitiStatus(Constant.ACTIVITI_NOT_OVER);
            activitiRecord.setInstanceId(processInstance.getId());
            activitiRecordService.saveOrUpdate(activitiRecord);
            map.clear();
            map.put("deploymentId", deploymentId);
            map.put("instanceId", processInstance.getId());
        } catch (Exception e) {
            map.clear();
            e.printStackTrace();
        }
        return map;
    }

}
